package com.tianci.appstore.ui.list;

import android.content.Context;
import android.view.View;

import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.widget.SlideFocusView;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;
import com.tianci.appstore.ui.list.ListBlock.MuitlSelectionListener;
import com.tianci.appstore.ui.widget.BlockFactory;
import com.tianci.appstore.ui.widget.SkyGridAdapter;
import com.tianci.appstore.utils.SkyConstants;

import java.util.HashMap;
import java.util.List;

public class SkyListAdapter extends SkyGridAdapter {
    protected List<SkyListAppData> mAppList;
    protected MuitlSelectionListener multiSelectListener;
    protected OnItemClickListener itemClickListener;
    protected SelectableListener selectableListener;
    protected int pageIndex = -1;
    protected ListType listType;
    protected boolean inSelectMode = false;
    private SlideFocusView.FocusChangedEvent mFocusChangedEvent = null;
    private SlideFocusView.FocusViewRevision mRevision = null;

    public SkyListAdapter(Context context, ListType listType, int pageIndex, float div) {
        // TODO Auto-generated constructor stub
        super(context, div, AdapterType.LIST_TYPE);
        this.listType = listType;
        this.pageIndex = pageIndex;
    }

    public void setData(int index, List<SkyListAppData> datas) {
        this.pageIndex = index;
        if (mAppList != null) {
            synchronized (mAppList) {
                this.mAppList = datas;
                this.count = mAppList.size();
                mAppItemList.clear();
                notifyDataSetChanaged();
            }
        } else {
            this.mAppList = datas;
            this.count = mAppList.size();
            mAppItemList.clear();
            notifyDataSetChanaged();
        }
    }

    public void setFocusChangedEvent(SlideFocusView.FocusChangedEvent focusChangedEvent) {
        this.mFocusChangedEvent = focusChangedEvent;
        mRevision = new SlideFocusView.FocusViewRevision(SkyConstants.Div(0), SkyConstants.Div(0));
    }

    @Override
    public View getView(int position, View convertView, boolean[] turnpage) {
        ListBlock appView = null;
        if (convertView == null) {
            LogUtils.d("uuu", "positionaaa = " + position);
            appView = (ListBlock) BlockFactory.createListBlock(mContext, listType);
            appView.setKeyListener(mKeyListener);
//            appView.setClickListener(mClickListener);
            if (itemClickListener != null)
                appView.setClickListener(itemClickListener);
            if (selectableListener != null)
                appView.setSelectableListener(selectableListener);
            appView.setMultiSelectListener(multiSelectListener);
            if (mFocusChangedEvent != null) {
                LogUtils.d("uuu", "positiobbb = " + position);
                mFocusChangedEvent.registerView(appView, mRevision, appView.getOnFocusChangeListener());
            }
        } else {
            appView = (ListBlock) convertView;
        }
        if (mAppList != null && mAppList.size() > position)
            appView.update(mAppList.get(position));
        else
            appView.clearData();
        appView.setChildrenMode(isChildrenMode);
        appView.setSelectMode(inSelectMode);
        appView.setIndex(position);
        appView.setTurnPage(turnpage);
        if (mFocusChangedEvent != null) {
            appView.setBackgroundDrawable(null);
        }
        mAppItemList.add(appView);
        return appView;
    }

    public void setMultiSelectListener(MuitlSelectionListener multiSelectListener) {
        this.multiSelectListener = multiSelectListener;
    }

    public void setInSelectMode(boolean inSelMode) {
        this.inSelectMode = inSelMode;
    }

    public void clearData(int pageIndex, int count) {
        this.pageIndex = pageIndex;
        this.mAppList = null;
        this.count = count;
        notifyDataSetChanaged();
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public boolean isDataFilled() {
        if (this.mAppList != null && this.mAppList.size() > 0)
            return true;
        return false;
    }

    public interface OnItemClickListener {
        public void onBlockClicked(View v, SkyListAppData data, int index);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.itemClickListener = listener;
    }

    public interface SelectableListener {
        public boolean checkSelectable(SkyListAppData data, int index);
    }

    public void setItemSelectableListener(SelectableListener listener) {
        this.selectableListener = listener;
    }

    public String[] getAppListNames() {
        if (mAppList != null && mAppList.size() > 0) {
            String[] mNames = new String[mAppList.size()];
            for (int i = 0; i < mAppList.size(); i++) {
                mNames[i] = mAppList.get(i).getTitle();
            }
            return mNames;
        }
        return null;
    }

    public HashMap<String, String[]> getAppListNames(HashMap<String, String[]> old) {
        if (mAppList != null && mAppList.size() > 0) {
            String[] mNames = new String[mAppList.size()];
            for (int i = 0; i < mAppList.size(); i++) {
                old.put(mAppList.get(i).getTitle(), new String[]{mAppList.get(i).getTitle()});
            }
        }
        return old;
    }
}
